#!/bin/bash
LANG=en_US.UTF-8

###Check if user is root
if [ $UID -ne 0 ]; then
    echo "Error: You must be root to run this script, please use root to install"
    exit 1
fi

#将SD卡扩展至实际容量
/usr/bin/rootfs-expand

#设置时区,启动自动同步时间
timedatectl set-timezone Asia/Shanghai
timedatectl set-ntp yes

rm -rf /home/go
rm -rf /usr/local/go

ps -ef|grep nodejs|grep -v grep|awk '{print $2}'|xargs kill -9
rm -rf /usr/local/nodejs

ps -ef|grep mysql|grep -v grep|awk '{print $2}'|xargs kill -9
rm -rf /usr/local/mysql
rm -rf /data/mysql
rm -f /etc/my.cnf
rm -f /etc/init.d/mysqld
rm -f /etc/profile.d/mysql.sh

ps -ef|grep gosuv|grep -v grep|awk '{print $2}'|xargs kill -9
rm -f /usr/local/bin/gosuv
rm -rf $HOME/.gosuv

#备份一下,修改CentOS-Base.repo位下面的内容

if [ -f "/etc/yum.repos.d/CentOS-Base.repo" ]; then
    repo=$(cat /etc/yum.repos.d/CentOS-Base.repo|grep mirrors.ustc.edu.cn)
    if [ "${repo}" = "" ];then
        cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
        curl https://gitee.com/rootsuper/pi4_centos7_repo/raw/master/CentOS-Base.repo -so /etc/yum.repos.d/CentOS-Base.repo --progress
        yum clean all
        yum makecache
    fi
fi

yum -y install gcc gcc-c++ kernel-devel
yum -y install wget git
yum -y update

Environment=/etc/profile

echo -e "----------------------------------------------------"
echo -e "\033[32m 开始安装Golang环境 \033[0m"
echo -e "----------------------------------------------------"

if [ ! -d "/usr/local/go" ]; then
    result=$(cat /etc/profile | grep /usr/local/go)
    if [ "${result}" = "" ];then
        wget https://dl.google.com/go/go1.13.5.linux-armv6l.tar.gz
        tar -zxvf go1.13.5.linux-armv6l.tar.gz -C /usr/local/ 
        rm -f go1.13.5.linux-armv6l.tar.gz
        cd /home
        mkdir go
        cd /home/go
        mkdir pkg src bin
        cd /home
        cat >> $Environment << \EOF

export GOROOT=/usr/local/go
export GOPATH=/home/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin:$GOBIN

EOF
        source $Environment
        echo -e "\033[32m golang install successfully. \033[0m"
    else
        echo -e "\033[31m golang is alreday installed. \033[0m"
    fi
else
    echo -e "\033[31m golang is alreday installed. \033[0m"
fi


echo -e "----------------------------------------------------"
echo -e "\033[32m 开始安装NodeJs环境 \033[0m"
echo -e "----------------------------------------------------"

if [ ! -d "/usr/local/nodejs" ]; then
    result=$(cat /etc/profile | grep /usr/local/nodejs)
    if [ "${result}" = "" ];then
        
        wget https://npm.taobao.org/mirrors/node/v9.11.2/node-v9.11.2-linux-armv7l.tar.gz
        tar -zxvf node-v9.11.2-linux-armv7l.tar.gz -C /usr/local/
        mv /usr/local/node-v9.11.2-linux-armv7l /usr/local/nodejs
        rm -f node-v9.11.2-linux-armv7l.tar.gz

        cat >> $Environment << \EOF

export NODEJS=/usr/local/nodejs
export PATH=$PATH:$NODEJS/bin

EOF
        source $Environment
        echo -e "\033[32m nodejs install successfully. \033[0m"
    else
        echo -e "\033[31m nodejs is alreday installed. \033[0m"
    fi
else
    echo -e "\033[31m nodejs is alreday installed. \033[0m"
fi

echo -e "----------------------------------------------------"
echo -e "\033[32m 开始安装 Mysql 5.7.28 环境 \033[0m"
echo -e "----------------------------------------------------"

mysqlInstall=/usr/local/mysql
mysqlDataBase=/data/mysql
mysqlDataDir=/data/mysql/data
mysqlLogdir=/data/mysql/logs
mysqlConfig=/etc/my.cnf

if [ ! -d "$mysqlInstall" ]; then
   
    wget https://raspberry-pi4.oss-cn-shenzhen.aliyuncs.com/mysql5.7.28.tar.gz
    tar -zxvf mysql5.7.28.tar.gz
    rm -f mysql5.7.28.tar.gz
    cp -r -v mysql /usr/local

    useradd mysql -s /sbin/nologin -M

    if [ ! -d "$mysqlDataDir" ]; then 
        mkdir -p  $mysqlDataDir
    fi

    if [ ! -d "$mysqlLogdir" ]; then 
        mkdir -p  $mysqlLogdir
    fi

    chown -R mysql:mysql $mysqlDataBase
    chown -R mysql:mysql $mysqlInstall

    if [ -f "$mysqlConfig" ]; then
        mv $mysqlConfig $mysqlConfig.`date +%Y%m%d%H%M%S`.bak
    fi

    cat > $mysqlConfig <<EOF

[client]
#password	= your_password
port		= 3306
socket		= /tmp/mysql.sock

[mysql]
no-auto-rehash

[mysqld]
port		= 3306
socket		= /tmp/mysql.sock
datadir = $mysqlDataDir
default_storage_engine = InnoDB
performance_schema_max_table_instances = 400
table_definition_cache = 400
skip-external-locking
key_buffer_size = 64M
max_allowed_packet = 100G
table_open_cache = 256
sort_buffer_size = 1M
net_buffer_length = 4K
read_buffer_size = 1M
read_rnd_buffer_size = 256K
myisam_sort_buffer_size = 16M
thread_cache_size = 32
query_cache_size = 32M
tmp_table_size = 64M
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

explicit_defaults_for_timestamp = true
#skip-name-resolve
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535

log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
slow_query_log=1
slow-query-log-file=$mysqlDataDir/mysql-slow.log
long_query_time=3
#log_queries_not_using_indexes=on
early-plugin-load = ""

log_error=$mysqlLogdir/error.log


innodb_data_home_dir = $mysqlDataDir
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = $mysqlDataDir
innodb_buffer_pool_size = 256M
innodb_log_file_size = 128M
innodb_log_buffer_size = 32M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 4
innodb_write_io_threads = 4

[mysqldump]
quick
max_allowed_packet = 500M

[myisamchk]
key_buffer_size = 64M
sort_buffer_size = 1M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
EOF

    yum install libaio-devel -y

    echo -e "\033[32m 正在初始化Mysql..... \033[0m"
    $mysqlInstall/bin/mysqld --initialize --user=mysql --basedir=$mysqlInstall  --datadir=$mysqlDataDir
    echo -e "\033[32m 初始化Mysql完成..... \033[0m"

    password=$(grep 'A temporary password' $mysqlLogdir/error.log |awk  '{print $NF}')

    echo -e "\033[32m 正在安装Mysql系统服务..... \033[0m"
    cp $mysqlInstall/support-files/mysql.server /etc/init.d/mysqld
    chkconfig --add mysqld
    chkconfig --list mysqld
    chkconfig mysqld on
    echo -e "\033[32m Mysql系统安装完成..... \033[0m"

    echo -e "\033[32m 正在启动Mysql服务..... \033[0m"
    /etc/init.d/mysqld start

    echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
    source /etc/profile.d/mysql.sh

    mysqlrootpwd=`cat /dev/urandom | head -n 16 | md5sum | head -c 10`
    
    /usr/local/mysql/bin/mysqladmin -uroot -p"$password" password $mysqlrootpwd

    echo -e "\033[32m 正在开启Mysql远程访问...... \033[0m"
    mysql -uroot -p"$mysqlrootpwd" -e "grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;"
    mysql -uroot -p"$mysqlrootpwd" -e "flush privileges;"
    echo -e "\033[32m Mysql远程访问开启成功 \033[0m"

    echo -e "\033[32m mysql 5.7.28 install completed \033[0m"

else
    echo -e "\033[31m mysql 5.7.28 is alreday installed. \033[0m"
fi


echo -e "----------------------------------------------------"
echo -e "\033[32m 开始安装GoSUV进程管理工具 \033[0m"
echo -e "----------------------------------------------------"

if [ ! -d "/usr/local/bin/gosuv" ]; then
    
    #git clone https://gitee.com/rootsuper/gosuv.git
    #cd gosuv
    #go generate
    #go build -tags vfs
    #mv gosuv /usr/local/bin
    #cd ..
    #rm -rf gosuv
    
    wget https://raspberry-pi4.oss-cn-shenzhen.aliyuncs.com/gosuv_linux_armv7.tar.gz
    tar -zxvf gosuv_linux_armv7.tar.gz -C /usr/local/bin
    rm -f gosuv_linux_armv7.tar.gz
    
    gosuvusr=`cat /dev/urandom | head -n 16 | md5sum | head -c 6`
    gosuvpwd=`cat /dev/urandom | head -n 16 | md5sum | head -c 6`

    gosuv start-server
    gosuv shutdown

    cat > "$HOME/.gosuv/conf/config.yml" <<EOF
server:
  httpauth:
    enabled: true
    username: $gosuvusr
    password: $gosuvpwd
  addr: :12345
  name: ""
  master: ""
client:
  server_url: http://$gosuvusr:$gosuvpwd@localhost:12345
EOF

    gosuv start-server

else
    echo -e "\033[31m GoSUV is alreday installed. \033[0m"
fi

source $Environment

HOST_IP=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6 | awk '{print $2}' | tr -d "addr:")

echo -e "=================================================================="
echo -e "\033[32m 恭喜你! 全部安装完成咯! \033[0m"
echo -e "=================================================================="
echo -e "Golang 当前版本:\033[32m$(go version)\033[0m"
echo -e "Nodejs 当前版本:\033[32m$(node -v)\033[0m"
echo -e "Mysql  当前版本:\033[32m$(mysql -V)\033[0m"
echo -e "Mysql  默认密码:\033[31m$mysqlrootpwd\033[0m"
echo -e "GoSUV Web Site:\033[32mhttp://$HOST_IP:12345\033[0m"
echo -e "GoSUV Web User:\033[32m$gosuvusr\033[0m"
echo -e "GoSUV Web Pass:\033[32m$gosuvpwd\033[0m"
echo -e "\033[33m注意:请保存好相关密码信息\033[0m"
echo -e "=================================================================="